package com.dailyblue.project.estate.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.Parking;
import com.dailyblue.project.estate.commons.handler.ParkingException;
import com.dailyblue.project.estate.commons.mapper.ParkingMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.parking.service.ParkingService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking> implements ParkingService {
    @Resource
    private ParkingMapper parkingMapper;

    @Override
    public JsonResult addParking(Parking parking) {
        boolean b = save(parking);
        if (!b) {
            throw new ParkingException("添加失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult findOneParking(String parkingId) {
        Parking parking = getById(parkingId);
        return ResultTool.success(parking);
    }

    @Cacheable(cacheNames = "findAllParkings")
    public JsonResult findAllParkings() {
        List<Parking> list = list();
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "findAllParkings", allEntries = true)
    public JsonResult deleteParking(String parkingId) {
        boolean b = removeById(parkingId);
        if (!b) {
            return ResultTool.fail(201, "删除失败");
        }
        return ResultTool.success("删除成功");
    }

    @CacheEvict(cacheNames = "findAllParkings", allEntries = true)
    public JsonResult updateParking(Parking parking) {
        UpdateWrapper<Parking> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", parking.getId());
        updateWrapper.set("community_id", parking.getCommunityId());
        updateWrapper.set("community_name", parking.getCommunityName());
        updateWrapper.set("code", parking.getCode());
        updateWrapper.set("name", parking.getName());
        updateWrapper.set("picture", parking.getPicture());
        updateWrapper.set("update_time", parking.getUpdateTime());
        int i = parkingMapper.update(updateWrapper);
        if (i <= 0) {
            return ResultTool.fail(201, "更新失败");
        }
        return ResultTool.success("更新成功");
    }
}
